﻿using UnityEngine;
using QFramework;
using System.Collections.Generic;
using System.Linq;

namespace HKTools
{
    public interface IHK_SimpleDialogSystem : ISystem
    {
        HK_SimpleDialog3D SimpleDialog3D { get; set; }

        // 当前对话名称
        string CurDialogName { get; set; }

        // 当前对话文本
        List<string> CurDialogRows { get; set; }

        /// <summary>
        /// 开启一个3D对话
        /// </summary>
        void StartNewDialog3D(string dialogName, bool isStartImmediately, HK_SimpleDialog3D simpleDialog3D);

        /// <summary>
        /// 推进对话
        /// </summary>
        void PlayForwardDialog();

        /// <summary>
        /// 结束当前3D对话
        /// </summary>
        void EndCurDialog3D();

        /// <summary>
        /// 选项后 根据索引 跳转对话
        /// </summary>
        void JumpDialog(int nextDialogIndex, string optionText);
    }

    public class HK_SimpleDialogSystem : AbstractSystem, IHK_SimpleDialogSystem
    {
        public HK_SimpleDialog3D SimpleDialog3D { get; set; }

        public string CurDialogName { get; set; }
        public List<string> CurDialogRows { get; set; }

        bool isRunning;

        // 当前对话索引值
        int curDialogIndex;

        protected override void OnInit() { }

        /// <summary>
        /// 开启一个3D对话
        /// </summary>
        public void StartNewDialog3D(string dialogName, bool isStartImmediately, HK_SimpleDialog3D simpleDialog3D)
        {
            this.CurDialogName = dialogName;

            TextAsset dialogText = Resources.Load<TextAsset>(this.CurDialogName);

            CurDialogRows = dialogText.text.Split('\n').ToList();

            curDialogIndex = 0;

            SimpleDialog3D = simpleDialog3D;

            SimpleDialog3D.Init();

            // 发送 对话开始 事件
            this.SendEvent(new Event_DialogStart { _dialogName = this.CurDialogName });

            isRunning = true;

            // 如果 立即开始，则直接弹出对话
            if (isStartImmediately)
                PlayForwardDialog();
        }

        /// <summary>
        /// 推进对话
        /// </summary>
        public void PlayForwardDialog()
        {
            if (!isRunning)
            {
                Debug.Log("<color=#E72E2E>----当前没有开启的对话!----</color>");
                return;
            }

            // 从 第二行 开始遍历
            for (int i = 1; i < CurDialogRows.Count; i++)
            {
                string[] cell = CurDialogRows[i].Split(',');

                if (int.Parse(cell[1]) != curDialogIndex)
                    continue;

                if (cell[0] == "#")
                {
                    SimpleDialog3D.OnDialogUpdate(cell[2], cell[3]);

                    curDialogIndex = int.Parse(cell[4]);
                }
                else if (cell[0] == "@")
                {
                    List<string> optionText_List = new List<string>();
                    List<int> nextDialogIndex_List = new List<int>();

                    for (int j = i; j < CurDialogRows.Count; j++)
                    {
                        if (CurDialogRows[j].Split(',')[0] == "@")
                        {
                            optionText_List.Add(CurDialogRows[j].Split(',')[3]);
                            nextDialogIndex_List.Add(int.Parse(CurDialogRows[j].Split(',')[4]));
                        }
                        else
                        {
                            break;
                        }
                    }

                    SimpleDialog3D.OnShowOptions(optionText_List, nextDialogIndex_List);
                }
                else if (cell[0] == "END")
                {
                    EndCurDialog3D();
                }

                break;
            }
        }

        /// <summary>
        /// 选项后 根据索引 跳转对话
        /// </summary>
        public void JumpDialog(int nextDialogIndex, string optionText)
        {
            curDialogIndex = nextDialogIndex;
            PlayForwardDialog();
            this.SendEvent(new Event_OptionChoiced { _optionText = optionText });
        }

        /// <summary>
        /// 结束当前3D对话
        /// </summary>
        public void EndCurDialog3D()
        {
            if (!isRunning)
            {
                Debug.Log("<color=#E72E2E>----当前没有开启的对话!----</color>");
                return;
            }

            isRunning = false;
            this.SendEvent(new Event_DialogEnd { _dialogName = CurDialogName });
            SimpleDialog3D.DeInit();
            SimpleDialog3D = null;
            CurDialogName = "";
            CurDialogRows = new List<string>();
        }
    }
}